﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMTVision.Widgets.Common;

namespace CMTVision.Widgets
{
    [System.Web.UI.ViewStateModeById]
    public class HtmlGenericControlModeById : System.Web.UI.HtmlControls.HtmlGenericControl
    {
        public HtmlGenericControlModeById()
            : base()
        { 
        }

        public HtmlGenericControlModeById(string tag)
            :base(tag)
        { 
        }

        public override System.Web.UI.Control FindControl(string id)
        {
            var control = base.FindControl(id);
            if (control == null)
            {
                if (id.StartsWith("Widget"))
                {
                    var template = this.NamingContainer;
                    while (!(template is ITemplate || template == null))
                    {
                        template = template.NamingContainer;
                    }
                    if (template is ITemplate)
                    {
                        var allColumns = new string[] { "Column1", "Column2", "Column3" };
                        foreach (string columnId in allColumns)
                        {
                            //if (columnId != ColumnID)
                            {
                                control = template.FindControl(columnId).FindControl(id);
                                if (control != null)
                                    break;
                            }
                        }
                    }
                }
            }
            return control; 
        }

        protected bool findControlForViewState;
        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
        }
    }
}